from socket import *

g_socketList = []
serSocket = socket(AF_INET,SOCK_STREAM)
serSocket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
localAddr = ("",7788)
serSocket.bind(localAddr)
serSocket.listen(5)
serSocket.setblocking(False)

while True:
    try:
        clientSocket,clientAddr = serSocket.accept()
        clientSocket.setblocking(False)
    except:
        pass
    else:
        print("一个新的客户端到来%s"%str(clientAddr))
        g_socketList.append((clientSocket,clientAddr))
    for cSocket,cAddr in g_socketList:
        try:
            recvData = cSocket.recv(1024)
            if len(recvData)>0:
                print("%s:%s"%(str(cAddr),recvData))
            else:
                cSocket.close()
                print("%s客户端已经关闭"%str(clientAddr))
                g_socketList.remove((cSocket,cAddr))
        except:
            pass
serSocket.close()